package com.xiaohao.springbootadmintemplate.config.securitycontext;

import com.xiaohao.springbootadmintemplate.filter.SecurityContextAuthenticationFilter;
import com.xiaohao.springbootadmintemplate.filter.SecurityUsernamePasswordContextFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    //根据用户名加载用户
    private UserDetailsServiceImpl userDetailsService;

    @Override
    //把userDetailsService让Security Content管理，同时设置密码加密方式
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(this.userDetailsService).passwordEncoder(new PasswordEncoder() {
            @Override
            public String encode(CharSequence charSequence) {
                return charSequence.toString();
            }

            @Override
            public boolean matches(CharSequence charSequence, String s) {
                System.out.println("matches======>"+charSequence.toString());
                return s.equals(charSequence.toString());
            }
        });
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        //具体配置
        httpSecurity
                .csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                //所有的OPTIONS请求全部放行不拦截
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                //公共URL全部放行不拦截
                .antMatchers(
                        "/*.html",
                        "/favicon.ico",
                        "/**/*.html",
                        "/**/*.css",
                        "/**/*.js",
                        "/login"
                ).permitAll()
                //其他的URL全部拦截认证
                .anyRequest().authenticated();

        //第一个添加鉴权过滤器，第二个添加登录过滤器
        httpSecurity
                .addFilter(new SecurityContextAuthenticationFilter(authenticationManager(),userDetailsService))
                .addFilter(new SecurityUsernamePasswordContextFilter(authenticationManager(), userDetailsService));
        httpSecurity.headers().cacheControl();
    }

}
